const jwt = require('jsonwebtoken')
const AUTHORIZATION = 'Authorization'
const expiresIn = 60 * 60		//单位为秒
const secret = 'secret'		//加密因子

const auth = {
	sign: (ctx, payload) => {
		const token = jwt.sign(payload, secret, { expiresIn })
		ctx.set(AUTHORIZATION, `Bearer ${token}`)
		// 可以将token写入cookie
		// ctx.cookies.set(‘token, token, {
		// 	maxAge: expiresIn,
		// 	httpOnly: true
		// })

		return token
	},

	verify: (ctx, decodeToken, token) => {
		let ret = true
		try {
			const payload = jwt.verify(token, secret)
			console.log('payload', payload)
			ret = false
		} catch (error) {
			console.log('error', error.name)
		}

		return ret
	}
}

module.exports = auth